from datetime import datetime
from hashlib import sha256
from json import dumps

from flask import Blueprint, request, jsonify, make_response, current_app
from flask_jwt_extended import create_access_token
from flask_restx import Api, Resource
from sqlalchemy import and_
from sqlalchemy.event import api

from models import user
from models.user import User

auth_bp = Blueprint("auth",__name__,url_prefix="/auth")
api = Api(auth_bp)


@auth_bp.route("/login", methods=['POST', 'GET'])
def login():
    req = request.get_json()
    password = sha256(req['password'].encode('utf-8')).hexdigest()
    print(password)
    user = User.query.filter_by(username=req['username'], password=password).first()
    if user:
        token = create_access_token(identity=user)
        return {'message': '登录成功','code':1, 'token': token, 'data':user}
    # password=request.json.get('password')
    return {'message': '登录失败', 'code': 0}
